/*
 * SensorsLoader.cpp
 *
 *  Created on: Feb 2, 2009
 *      Author: Andrzej Olchawa
 *      mailto: andrzejolchawa@gmail.com
 *      http://www.hardtechnology.org
 *      This is a part of the Armazd project, which is covered by
 *      GNU GPL v2 license.
 */

#include "SensorsLoader.h"
#include "Agent.h"

SensorsLoader::SensorsLoader()
{
	sensorsLocation = AgentConfiguration::getSensorsConfigPath();
}

SensorsLoader::~SensorsLoader() {}

string SensorsLoader::getSensorsLocation()
{
	return sensorsLocation;
}

list<Sensor*> *SensorsLoader::getListOfSensors()
{
	return listOfSensors;
}

void SensorsLoader::setSensorsLocation(string path)
{
	sensorsLocation = path;
}

list<Sensor*> *SensorsLoader::setListOfSensors(list<Sensor*> *sensorsList)
{
	listOfSensors = sensorsList;
	return listOfSensors;
}

list<Sensor*> *SensorsLoader::loadSensors(list<Sensor*> *sensorsList)
{
	if (sensorsList)
	{
		setListOfSensors(sensorsList);
		try
		{
			loadSensors();
		}
		catch (AgentException &e)
		{
			e.printException();
		}
	}
	else
	{
		throw AgentException("list<Sensor*> *SensorsLoader.loadSensors: sensorsList is null");
	}
	return listOfSensors;
}

void SensorsLoader::loadSensors()
{
	HTProperties properties;
	try
	{
		properties.loadProperties(sensorsLocation);
		for (int i=0; i<properties.getNumberOfElements(); i++)
		{
			string sensorName, sensorPath;
			int sensorDelay;

			try
			{
				sensorName = properties[i].key;
				AgentUtils::extractSensorInfoFromConfigLine(sensorPath,
						sensorDelay, properties[i].value, i+1);
			}
			catch (AgentException &e)
			{
				e.printException();
			}
			catch (HTException &aoe)
			{
				aoe.printException();
			}

			Sensor *sensor = new Sensor(sensorName, sensorPath, sensorDelay);
			listOfSensors->push_back(sensor);
		}
	}
	catch (HTException &aoe)
	{
		aoe.printException();
	}
}

void SensorsLoader::destroySensors() {}
